package com.zhoug.zmui3demo.ui;

import android.os.Bundle;
import android.text.Editable;
import android.view.Gravity;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;

import com.zhoug.common3.content.FileType;
import com.zhoug.common3.utils.FileUtils;
import com.zhoug.common3.utils.StringUtils;
import com.zhoug.common3.utils.ToastUtils;
import com.zhoug.common3.utils.file.IFile;
import com.zhoug.zmui3.ZMUIFilePreview;
import com.zhoug.zmui3.task.TaskTimeFlowData;
import com.zhoug.zmui3.task.ZMUITaskTimeFlowView;
import com.zhoug.zmui3demo.R;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

/**
 * @Author: zhoug
 * @Date: 2024-07-17
 * @Description:
 */
    public class ZMUITaskTimeFlowViewActivity extends AppCompatActivity {

    private EditText editRatio;
    private Button btnConfirm;
    private Switch switchAutoTextSize;
    private RadioButton radioLeft;
    private RadioButton radioCenter;
    private RadioButton radioRight;
    private RadioGroup radioGroupTimeGravity;
    private ZMUITaskTimeFlowView taskTImeFlowView1;
    private ZMUITaskTimeFlowView taskTImeFlowView2;
    private ZMUITaskTimeFlowView taskTImeFlowView3;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task_time_flow_view);
        findViews();
        init();

    }

    private void init() {
        switchAutoTextSize.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                taskTImeFlowView1.setAutoTextSize(isChecked);
                taskTImeFlowView2.setAutoTextSize(isChecked);
                taskTImeFlowView3.setAutoTextSize(isChecked);
                refresh();

            }
        });
        radioGroupTimeGravity.check(R.id.radio_center);
        radioGroupTimeGravity.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.radio_left) {
                    taskTImeFlowView1.setTimeGravity(Gravity.START);
                    taskTImeFlowView2.setTimeGravity(Gravity.START);
                    taskTImeFlowView3.setTimeGravity(Gravity.START);
                } else if (checkedId == R.id.radio_right) {
                    taskTImeFlowView1.setTimeGravity(Gravity.END);
                    taskTImeFlowView2.setTimeGravity(Gravity.END);
                    taskTImeFlowView3.setTimeGravity(Gravity.END);
                } else {
                    taskTImeFlowView1.setTimeGravity(Gravity.CENTER);
                    taskTImeFlowView2.setTimeGravity(Gravity.CENTER);
                    taskTImeFlowView3.setTimeGravity(Gravity.CENTER);
                }
                refresh();

            }
        });

        btnConfirm.setOnClickListener(v -> {
            Editable text = editRatio.getText();
            if (text != null) {
                String trim = text.toString().trim();
                if (!StringUtils.isNullOrEmpty(trim)) {
                    try {
                        float ratio = Float.parseFloat(trim);
                        taskTImeFlowView1.setLeftRightRatio(ratio);
                        taskTImeFlowView2.setLeftRightRatio(ratio);
                        taskTImeFlowView3.setLeftRightRatio(ratio);
                        refresh();
                    } catch (NumberFormatException e) {

                    }
                }
            }
        });


        taskTImeFlowView1.setOnNodeClickListener(onNodeClickListener);
        taskTImeFlowView1.setOnExtraButtonClickListener(onExtraButtonClickListener);
        taskTImeFlowView1.setOnLineDataClickListener(onLineDataClickListener);
        taskTImeFlowView1.setOnTimeClickListener(onTimeClickListener);

        taskTImeFlowView2.setOnNodeClickListener(onNodeClickListener);
        taskTImeFlowView2.setOnExtraButtonClickListener(onExtraButtonClickListener);
        taskTImeFlowView2.setOnLineDataClickListener(onLineDataClickListener);
        taskTImeFlowView2.setOnTimeClickListener(onTimeClickListener);

        taskTImeFlowView3.setOnNodeClickListener(onNodeClickListener);
        taskTImeFlowView3.setOnExtraButtonClickListener(onExtraButtonClickListener);
        taskTImeFlowView3.setOnLineDataClickListener(onLineDataClickListener);
        taskTImeFlowView3.setOnTimeClickListener(onTimeClickListener);

        taskTImeFlowView3.setOnPreviewFileClickListener(new ZMUIFilePreview.OnPreviewFileClickListener() {
            @Override
            public void onPreviewFileClick(ZMUIFilePreview view, List<IFile> data, int position) {
                ToastUtils.toastLong("自定义实现文件预览接口");
            }
        });

        refresh();

    }


    private void refresh() {
        taskTImeFlowView1.setData(requestData(1));
        taskTImeFlowView2.setData(requestData(2));
        taskTImeFlowView3.setData(requestData(3));
    }

    private final ZMUITaskTimeFlowView.OnNodeClickListener onNodeClickListener = new ZMUITaskTimeFlowView.OnNodeClickListener() {
        @Override
        public void onNodeClick(ZMUITaskTimeFlowView view, TaskTimeFlowData data) {
            String name = data.getName();
            ToastUtils.toastLong("点击Node:"+name);
        }
    };

    private final ZMUITaskTimeFlowView.OnExtraButtonClickListener onExtraButtonClickListener = new ZMUITaskTimeFlowView.OnExtraButtonClickListener() {
        @Override
        public void onExtraButtonClick(ZMUITaskTimeFlowView view, TaskTimeFlowData data) {
            String name = data.getExtrasName();
            ToastUtils.toastLong("点击:"+name);
        }
    };

    private final ZMUITaskTimeFlowView.OnLineDataClickListener onLineDataClickListener = new ZMUITaskTimeFlowView.OnLineDataClickListener() {
        @Override
        public void onLineDataClick(ZMUITaskTimeFlowView view, TaskTimeFlowData data) {
            String name = data.getName();
            ToastUtils.toastLong("点击Line:"+name);
        }
    };

    private final ZMUITaskTimeFlowView.OnTimeClickListener onTimeClickListener=new ZMUITaskTimeFlowView.OnTimeClickListener() {
        @Override
        public void onTimeClick(ZMUITaskTimeFlowView view, TaskTimeFlowData data) {
            String name = data.getName();
            String time = data.getTime();
            ToastUtils.toastLong("点击时间:"+name+":"+time);
        }
    };


    private List<TaskTimeFlowData> requestData(int index) {

        List<TaskTimeFlowData> data = new ArrayList<>();
        //1
        TaskTimeFlowData taskTimeFlowData = new TaskTimeFlowData();
        taskTimeFlowData.setTime("2024-07-17 12:35:26");
        taskTimeFlowData.setName("违规");
        if (index == 1) {
            taskTimeFlowData.setComplete(false);
        } else {
            taskTimeFlowData.setComplete(true);
        }
        List<TaskTimeFlowData.LineData> lines = new ArrayList<>();
        TaskTimeFlowData.FileLineData fileLineData = new TaskTimeFlowData.FileLineData();
        fileLineData.setName("违规材料");
        fileLineData.setFiles(getFile(0));
        lines.add(fileLineData);
        taskTimeFlowData.setLines(lines);
        data.add(taskTimeFlowData);
        if (index == 1) {
            return data;
        }
        //2
        taskTimeFlowData = new TaskTimeFlowData();
        taskTimeFlowData.setTime("2024-07-18 08:12:00");
        taskTimeFlowData.setName("已整改");
        taskTimeFlowData.setComplete(true);
        lines = new ArrayList<>();
        TaskTimeFlowData.TextLineData textLineData = new TaskTimeFlowData.TextLineData();
        textLineData.setName("整改人");
        textLineData.setText("王东");
        lines.add(textLineData);
        fileLineData = new TaskTimeFlowData.FileLineData();
        fileLineData.setName("整改附件");
        fileLineData.setFiles(getFile(2));
        lines.add(fileLineData);
        taskTimeFlowData.setLines(lines);
        data.add(taskTimeFlowData);
        //3
        taskTimeFlowData = new TaskTimeFlowData();

        if (index == 2) {
            taskTimeFlowData.setName("待审核");
            taskTimeFlowData.setComplete(false);
            taskTimeFlowData.setExtrasName("催一下");
//            taskTimeFlowData.setExtrasIcon(R.drawable.zmui_icon_write);
            taskTimeFlowData.setExtrasEnable(true);
            data.add(taskTimeFlowData);
        } else {
            taskTimeFlowData.setTime("2024-07-18 16:00:35");
            taskTimeFlowData.setName("已审核");
            taskTimeFlowData.setComplete(true);
            lines = new ArrayList<>();
            textLineData = new TaskTimeFlowData.TextLineData();
            textLineData.setName("审核人");
            textLineData.setText("马冬梅");
            lines.add(textLineData);
            textLineData = new TaskTimeFlowData.TextLineData();
            textLineData.setName("审核结果");
            textLineData.setText("合格");
            lines.add(textLineData);
            fileLineData = new TaskTimeFlowData.FileLineData();
            fileLineData.setName("审核附件");
            fileLineData.setFiles(getFile(1));
            lines.add(fileLineData);
            taskTimeFlowData.setLines(lines);
            data.add(taskTimeFlowData);
        }

        return data;
    }


    private List<IFile> getFile(int index) {
        File externalRoot = FileUtils.getExternalRoot();
        List<IFile> files = new ArrayList<>();
        if (index == 0 || index == 2) {
            files.add(new MyFile(FileType.IMAGE, new File(externalRoot, "0相册/2018-10-14_17_56_07.jpg").getAbsolutePath(), ".jpg", "本地图片"));
        }
        if (index == 1 || index == 2) {
            files.add(new MyFile(FileType.PDF, new File(externalRoot, "0相册/011002300411-64937955.pdf").getAbsolutePath(), ".pdf", "PDF"));
        }
        if (index == 2) {
            files.add(new MyFile(FileType.IMAGE, new File(externalRoot, "0相册/1549364720765.jpeg").getAbsolutePath(), ".jpg", "本地图片"));
            files.add(new MyFile(FileType.IMAGE, new File(externalRoot, "0相册/1549364718813.jpeg").getAbsolutePath(), ".jpg", "本地图片"));
            files.add(new MyFile(FileType.IMAGE, new File(externalRoot, "0相册/1549364718758.jpeg").getAbsolutePath(), ".jpg", "本地图片"));
        }

        return files;
    }

    private void findViews() {
        editRatio = findViewById(R.id.edit_ratio);
        btnConfirm = findViewById(R.id.btn_confirm);
        switchAutoTextSize = findViewById(R.id.switch_autoTextSize);
        radioLeft = findViewById(R.id.radio_left);
        radioCenter = findViewById(R.id.radio_center);
        radioRight = findViewById(R.id.radio_right);
        radioGroupTimeGravity = findViewById(R.id.radioGroup_timeGravity);
        taskTImeFlowView1 = findViewById(R.id.taskTImeFlowView1);
        taskTImeFlowView2 = findViewById(R.id.taskTImeFlowView2);
        taskTImeFlowView3 = findViewById(R.id.taskTImeFlowView3);
    }

    private class MyFile implements IFile {
        private int fileType;
        private String realUrl;
        private String fileSuffix;
        private String fileName;

        public MyFile(int fileType, String realUrl, String fileSuffix, String fileName) {
            this.fileType = fileType;
            this.realUrl = realUrl;
            this.fileSuffix = fileSuffix;
            this.fileName = fileName;
        }

        @Override
        public int getFileType() {
            return fileType;
        }

        @Override
        public String getRealUrl() {
            return realUrl;
        }

        @Override
        public String getFileSuffix() {
            return fileSuffix;
        }

        @Override
        public String getFileName() {
            return fileName;
        }
    }

}
